package fzw.jsmx.aptlib_processor

import fzw.jsmx.aptlib_anno.InitActivity
import fzw.jsmx.aptlib_anno.InitModel
import java.util.*

object ProcessUtil {

    /**
     * 生成model类
     * @param cla 包名
     * @param simple 类名
     * @param a 注解
     */
    fun createModel(cla: String, simple: String, a: InitModel): String {
        val p = a.presenter.isNotEmpty()
        val builder = StringBuilder()
        builder.append("package $cla;\n\n")
            .append("import androidx.annotation.NonNull;\nimport androidx.lifecycle.ViewModel;\nimport androidx.lifecycle.ViewModelProvider;\n\n")
            .append("public class $simple implements ViewModelProvider.Factory {\n\n")
        if (p) {
            builder.append("    private ${a.presenter} p;\n\n")
                .append("    public $simple(${a.presenter} p) {\n")
                .append("        this.p = p;\n    }\n\n")
        }
        builder.append("    @NonNull\n    @Override\n")
            .append("    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {\n")
        if (p) {
            builder.append("        return (T) new ${a.name}(p);\n    }\n\n}")
        } else {
            builder.append("        return (T) new ${a.name}();\n    }\n\n}")
        }
        return builder.toString()
    }

    fun createActivity(cla: String, simple: String, a: InitActivity): String {
        val low = a.name.toLowerCase(Locale.ROOT)
        val builder = StringBuilder()
        builder.append("package $cla;\n\n")
            .append("import fzw.jsmx.androidapt.R;\nimport androidx.appcompat.app.AppCompatActivity;\nimport fzw.jsmx.aptlib_anno.InitModel;\nimport androidx.annotation.Nullable;\nimport android.os.Bundle;\n")
            .append("import fzw.jsmx.androidapt.databinding.Activity${a.name}Binding;\nimport org.jetbrains.annotations.NotNull;\nimport android.app.Activity;\n\n")
            .append("@InitModel(name = \"Round_${a.name}\", presenter = \"${a.name}Presenter\")\n")
            .append("public class $simple extends AppCompatActivity implements ${a.name}Presenter{\n\n")
            .append("    private Round_${a.name} $low;\n")
            .append("    private Activity${a.name}Binding layout;\n\n")
            .append("    @Override\n    protected void onCreate(@Nullable Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n")
            .append("        layout = Activity${a.name}Binding.inflate(getLayoutInflater());\n")
            .append("        setContentView(layout.getRoot());\n")
            .append("        $low = new Round_${a.name}(this);\n")
            .append("        $low.initialize();\n")
            .append("        $low.checkPermission(this);\n")
            .append("   }\n")
            .append(
                "\n" +
                        "    @Override\n" +
                        "    protected void onResume() {\n" +
                        "        super.onResume();\n" +
                        "        flash.resume();\n" +
                        "    }\n" +
                        "\n" +
                        "    @Override\n" +
                        "    protected void onPause() {\n" +
                        "        super.onPause();\n" +
                        "        flash.pause();\n" +
                        "    }\n" +
                        "\n" +
                        "    @NotNull\n" +
                        "    @Override\n" +
                        "    public ActivityFlashBinding layout() {\n" +
                        "        return layout;\n" +
                        "    }\n" +
                        "\n" +
                        "    @NotNull\n" +
                        "    @Override\n" +
                        "    public Activity get() {\n" +
                        "        return this;\n" +
                        "    }\n"
            )
            .append("}")
        return builder.toString()
    }

}